<?php
// dirname()返回当前文件或目录的上级目录名称
define('ROOT_DIR', dirname(__DIR__)); 
define('PUBLIC_DIR', __DIR__);
define('ACTION_DIR', ROOT_DIR . DIRECTORY_SEPARATOR . 'action');
define('VIEW_DIR', ROOT_DIR . DIRECTORY_SEPARATOR . 'view');

require_once ROOT_DIR . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'data.php';

session_start();

/**
 * 获取action程序脚本文件url
 *
 * @param string $action action文件名
 */
function url($action)
{
   if ($_SERVER['DOCUMENT_ROOT'] == PUBLIC_DIR) {
       return 'http://' . $_SERVER['HTTP_HOST'] . '/' . $action;
   }
   $appName = strrchr(ROOT_DIR, DIRECTORY_SEPARATOR);
   return 'http://' . $_SERVER['HTTP_HOST'] . $appName  . '/public/' . $action;
}

/**
 * 获取资源文件（img,css,js）的url
 *
 * @param string $res 资源文件名
 */
function assets($res)
{
   // 部署到生产环境下的资源文件路径
   if ($_SERVER['DOCUMENT_ROOT'] == PUBLIC_DIR) {
      return 'http://' . $_SERVER['HTTP_HOST'] . '/' . $res;
   }
   // 本地开发环境下的资源文件路径
   $appName = strrchr(ROOT_DIR, DIRECTORY_SEPARATOR);
   return 'http://' . $_SERVER['HTTP_HOST'] . $appName  . '/public/' . $res;
}

// 获取当前请求的url
$url = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
$action = strrchr($url, '/');
$action = substr($action, strpos($action, '/') + 1);
$action = empty($action) ? 'index' : $action; 
$actionFile = ACTION_DIR . DIRECTORY_SEPARATOR .  $action . '.php';
require $actionFile;